﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Harmonic.CommandHost.Commands;

namespace Harmonic.CommandHost
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class CqrsService : ICqrsService
    {
        //public void SubmitCommand(string jsonCommand, string commandType)
        //{
        //    Harmonic.Infrastructure.ICommand command = (ICommand)JsonConvert.DeserializeObject(jsonCommand, Type.GetType(commandType));
        //    ServiceLocator.CommandBus.Send(command);
        //}

        public void RegisterNewUser(string name, string email, string password, int originalVersion)
        {
            try
            {                
                RegisterNewUser register = new RegisterNewUser(Guid.NewGuid(), name, email, password, originalVersion);
                ServiceLocator.CommandBus.Send(register);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public void UpgradeUser(Guid id, string reason, int originalVersion)
        {
            try
            {

                UpgradeUser upgrade = new UpgradeUser(id, reason, originalVersion);
                ServiceLocator.CommandBus.Send(upgrade);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public void DowngradeUser(Guid id, string reason, int originalVersion)
        {
            try
            {

                DowngradeUser downgrade = new DowngradeUser(id, reason, originalVersion);
                ServiceLocator.CommandBus.Send(downgrade);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public void DeleteUser(Guid id, string reason, int originalVersion)
        {
            try
            {
                DeleteUser delete = new DeleteUser(id, reason, originalVersion);
                ServiceLocator.CommandBus.Send(delete);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
    }
}
